package tests;

import common.Utils;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;

/**
 * @Author xuejiao
 * Data 2025/5/30 11:07
 */
public class RegisterAdminPage extends Utils {

    public static String url = "http://localhost:8080/register.html?admin=true";

    public RegisterAdminPage() {
        super(url);
    }

    //注册页面加载是否正确
    public void registerPageRight() throws InterruptedException {
        driver.findElement(By.cssSelector("body > div > h2"));


    }

    //成功注册
    public void registerSuccess() throws InterruptedException {


        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));

        // 1. 打印当前页面 URL 和 HTML，调试时很重要
        System.out.println("当前URL: " + driver.getCurrentUrl());
        System.out.println("页面源码预览: " + driver.getPageSource().substring(0, 500)); // 打印前500字符
        String currentUrl = driver.getCurrentUrl();
        if(!currentUrl.contains("register.html")) {
            // 2. 等待并点击“注册”链接（建议用 By.linkText）
            WebElement registerLink = wait.until(ExpectedConditions.elementToBeClickable(
                    By.linkText("注册")  // 更符合你HTML中的 <a href="register.html?admin=true">注册</a>
            ));
            registerLink.click();

            // 3. 等待跳转页面的URL包含 register.html
            wait.until(ExpectedConditions.urlContains("register.html"));
        }
        // 4. 等待表单加载完成
        wait.until(ExpectedConditions.presenceOfElementLocated(By.id("registerForm")));

        // 5. 填表单并提交
        driver.findElement(By.id("name")).sendKeys("luccczi");
        driver.findElement(By.id("mail")).sendKeys("luz21i@qq.com");
        driver.findElement(By.id("phoneNumber")).sendKeys("17789032539");
        driver.findElement(By.id("password")).sendKeys("luzi123");
        driver.findElement(By.cssSelector("#registerForm > button")).click();

        // 6. 等待弹窗并验证
        Alert alert = wait.until(ExpectedConditions.alertIsPresent());
        String text = alert.getText();
        System.out.println("弹窗内容：" + text);
        assert text.equals("注册成功，去登录！");
        alert.accept();


    }


    //注册失败
    public void registerFail() throws InterruptedException {
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(20));
        String currentUrl = driver.getCurrentUrl();
        if (!currentUrl.contains("register.html")) {

            WebElement registerLink = wait.until(ExpectedConditions.elementToBeClickable(
                    By.linkText("注册")  // 更符合你HTML中的 <a href="register.html?admin=true">注册</a>
            ));
            registerLink.click();
            wait.until(ExpectedConditions.urlContains("register.html"));
        }
        wait.until(ExpectedConditions.presenceOfElementLocated(By.id("registerForm")));
        driver.findElement(By.id("name")).sendKeys("znh");
        driver.findElement(By.id("mail")).sendKeys("luzi@qq.com");
        driver.findElement(By.id("phoneNumber")).sendKeys("13345678902");
        driver.findElement(By.id("password")).sendKeys("l342423");
        Thread.sleep(1000);
        driver.findElement(By.cssSelector("#registerForm > button")).click();
        Alert alert = wait.until(ExpectedConditions.alertIsPresent());
        String text = alert.getText();
        System.out.println("弹窗内容：" + text);
        assert text.equals("注册失败! 邮箱被使用");
        alert.accept();


        driver.navigate().back();

    }

}
